<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="warning"  @click="batch_export">导出</el-button>
                <div class="finish">
                    <div class="finishNum">总欠款:
                        <div class="text">{{ total_stock.collect_money }}</div>
                    </div>
                    <div class="finishNum">总收回金额:
                        <div class="text">{{ total_stock.pay_money }}</div>
                    </div>
                    <div class="finishNum">总应收金额:
                        <div class="text">{{ total_stock.only_money }}</div>
                    </div>
                </div>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-select v-model="search.customer_id" filterable clearable placeholder="请选择客户">
                        <el-option v-for="item in clientList" :key="item.id" :label="item.customer_name" :value="item.id"/>
                    </el-select>
                    <el-date-picker v-model="search.business_date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                    <el-button type="primary" icon="el-icon-search" @click="getData"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <!-- <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border @setExtend="setExtend" :params="query">
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
                <el-table-column label="业务日期" prop="business_date" align="center"></el-table-column>
                <el-table-column label="单据编号" prop="contract.contract_code" align="center">
                    <template #default="scope">
                        <div v-if="scope.row.contract_id != 0">
                            {{scope.row.contract && scope.row.contract.contract_code}}
                        </div>
                        <div v-if="scope.row.contract_id == 0">
                            {{scope.row.contractReturn && scope.row.contractReturn.return_code}}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="业务类型" prop="type" align="center">
                    <template #default="scope">
                        {{scope.row.type == 1?'销售订单':scope.row.type == 2?'销售退货':'其他'}}
                    </template>
                </el-table-column>
                <el-table-column label="期别" prop="money" align="center">
                    <template #default="scope">
                        <div v-for="item in stages_all" :key="item.stages_number">
                            {{scope.row.stages_number == item.stages_number?item.type:''}}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="欠款总额" prop="money" align="center"></el-table-column>
                <el-table-column label="收回欠款" prop="pay_money" align="center"></el-table-column>
                <el-table-column label="应收欠款" prop="only_money" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
            </scTable> -->
            <scTable ref="table" row-key="id" :data="data" @selection-change="selectionChange" hideDo hidePagination border>
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
                <el-table-column label="业务日期" prop="business_date" align="center"></el-table-column>
                <el-table-column label="单据编号" prop="contract.contract_code" align="center">
                    <template #default="scope">
                        <div v-if="scope.row.contract_id != 0">
                            {{scope.row.contract && scope.row.contract.contract_code}}
                        </div>
                        <div v-if="scope.row.contract_id == 0">
                            {{scope.row.contractReturn && scope.row.contractReturn.return_code}}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="明细表" type="expand" prop="expand" align="center" width="70">
                    <template #default="props">
                        <div m="4">
                            <scTable :data="props.row.detail" hidePagination hideDo border>
                                <el-table-column width="200" />
                                <el-table-column label="商品名称" prop="goods.goods_name" align="center"></el-table-column>
                                <el-table-column label="商品编码" prop="goods.goods_code" align="center"></el-table-column>
                                <el-table-column label="规格型号" prop="goods.goods_spec" align="center"></el-table-column>
                                <el-table-column label="分类" prop="goods.cate.cate_name" align="center"></el-table-column>
                                <el-table-column label="单位" prop="goods.unit.unit_name" align="center"></el-table-column>
                                <el-table-column label="销售数量" prop="number" align="center"></el-table-column>
                                <el-table-column label="未税单价" prop="price" align="center"></el-table-column>
                                <el-table-column label="含税单价" prop="tax_price" align="center"></el-table-column>
                                <el-table-column label="税率" prop="tax_rate" align="center"></el-table-column>
                                <el-table-column label="税额" align="center">
                                    <template #default="scope">
                                        {{ scope.row.tax_rate && scope.row.price && scope.row.number ?
                                        (((1 + (Number(scope.row.tax_rate) / 100)) * Number(scope.row.price)* Number(scope.row.number)) - ((Number(scope.row.price) * Number(scope.row.number)))).toFixed(2) : 0 }}
                                    </template>
                                </el-table-column>
                                <el-table-column label="未税合计" align="center">
                                    <template #default="scope">
                                        {{ (Number(scope.row.price)* Number(scope.row.number)).toFixed(2)}}
                                    </template>
                                </el-table-column>
                                <el-table-column label="含税合计" prop="total_price" align="center"></el-table-column>
                            </scTable>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="业务类型" prop="type" align="center">
                    <template #default="scope">
                        {{scope.row.type == 1?'销售订单':scope.row.type == 2?'销售退货':'其他'}}
                    </template>
                </el-table-column>
                <el-table-column label="期别" prop="money" align="center">
                    <template #default="scope">
                        <div v-for="item in stages_all" :key="item.stages_number">
                            {{scope.row.stages_number == item.stages_number?item.type:''}}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="欠款总额" prop="money" align="center"></el-table-column>
                <el-table-column label="收回欠款" prop="pay_money" align="center"></el-table-column>
                <el-table-column label="应收欠款" prop="only_money" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
            </scTable>
        </el-main>
        <el-footer style="height: 51px;padding: 9px 15px" class="scTable-page">
            <div class="scTable-pagination">
                <el-pagination background small layout="total,sizes,prev, pager, next,jumper" v-model:page-size="pageSize" :page-sizes="[10,20,30,50]" :total="Number(countSize)" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
            </div>
        </el-footer>
    </el-container>
</template>

<script>

export default {
    name: '应收账款（列表）',
    components: {
    },
    data() {
        return {
            apiObj: this.$API.finance.receivable.detail,
            selection: [],
            search: {
                customer_id: null,
                business_date: null,
            },
            customer_id: "",
            clientList: [],
            total_stock: {
                collect_money: 0,
                pay_money: 0,
                only_money: 0,
            },
            query: {
                need_extend: 1
            },
            stages_all: [
                { stages_number: 0,type: '一次性',},
                { stages_number: 1,type: '首期金额',},
                { stages_number: 2,type: '二期金额',},
                { stages_number: 3,type: '三期金额',},
                { stages_number: 4,type: '四期金额',},
                { stages_number: 5,type: '五期金额',},
                { stages_number: 6,type: '六期金额',},
                { stages_number: 7,type: '七期金额',},
                { stages_number: 8,type: '八期金额',},
                { stages_number: 9,type: '九期金额',},
                { stages_number: 10,type: '十期金额',},
                { stages_number: 11,type: '十一期金额',},
                { stages_number: 12,type: '十二期金额',},
            ],
            data: [],
            countSize:0,
            pageSize:20,
        }
    },
    mounted() {
        this.getClient();
        this.getData();
    },
    methods: {
        setExtend(data) {
            this.total_stock = data
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.data = []
            this.total_stock = {}
        },
        // 客户
        async getClient() {
            var res = await this.$API.client.client.all.get();
            this.clientList = res.data;
        },
        /**导出*/
		async batch_export() {
            let query = {
                business_date: this.search.business_date,
                customer_id: this.search.customer_id,
                need_extend: 1
            }
			var res = await this.$API.finance.receivable.excel_account.get(query);
			if (res.code == 1) {
				this.$message.success("导出成功");
				window.location.href = res.data.url
			} else {
				this.$alert(res.message, "提示", {type: "error"});
			}
		},
        // 获取表，图片列表
        async getData(){
            let query = {
                pageSize: 20,
                need_extend: 1,
                business_date: this.search.business_date,
                customer_id: this.search.customer_id,
            }
            var res = await this.$API.finance.receivable.detail.get(query);
            if(res.code == 1){
                this.countSize = res.data.total
                this.data = res.data.rows
                this.total_stock.collect_money = res.data.extend.collect_money
                this.total_stock.pay_money = res.data.extend.pay_money
                this.total_stock.only_money = res.data.extend.only_money
            }
        },
        // 分页每页数量
        async handleSizeChange(size){
            let query = {
                pageSize:size,
                need_extend: 1,
                business_date: this.search.business_date,
                customer_id: this.search.customer_id,
            }
            this.pageSize = size
            var res = await this.$API.finance.receivable.detail.get(query);
            if(res.code == 1){
                this.data = res.data.rows
            }
        },
        // 分页操作
        async handleCurrentChange(currentPage) {
            let query = {
                pageSize:this.pageSize,
                page:currentPage,
                need_extend: 1,
                business_date: this.search.business_date,
                customer_id: this.search.customer_id,
            }
            var res = await this.$API.finance.receivable.detail.get(query);
            if(res.code == 1){
                this.data = res.data.rows
            }
        },
    }
}
</script>

<style></style>
